package com.findfuture.demo.controller;

import com.findfuture.demo.model.Albums;
import com.findfuture.demo.model.Catalog;
import com.findfuture.demo.service.IAlbumService;
import com.findfuture.demo.service.ICatalogService;
import com.findfuture.demo.service.IImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Alex
 * Date: 12-2-5
 * Time: 下午6:10
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private ICatalogService catalogService;

    @Autowired
    private IImageService imageService;

    @Autowired
    private IAlbumService albumService;

    @RequestMapping("/gallery")
    public String gallery(ModelMap modelMap) throws Exception {
        modelMap.addAttribute("images", imageService.getImagesByWhere(" and top !=1 order by id desc", 1, 4));
        modelMap.addAttribute("topImages", imageService.getImagesByWhere(" and top = 1", 1, 4));
        modelMap.addAttribute("catalogs", catalogService.getCatalogList());
        return "forward:/gallery.jsp";
    }

    @RequestMapping("/productShow-{imageId}")
    public String productShow(ModelMap modelMap, @PathVariable int imageId) {
        modelMap.addAttribute("image", imageService.getImageById(imageId));
        modelMap.addAttribute("catalogs", catalogService.getCatalogList());
        return "forward:/productShow.jsp";
    }

    @RequestMapping("/products-{id}-{page}")
    public String products(ModelMap modelMap, @PathVariable int id, @PathVariable int page) throws Exception {

        List<Catalog> catalogs = catalogService.getCatalogList();
        if (id == 0) {
            if (catalogs != null && catalogs.size() > 0) {
                id = catalogs.get(0).getId();
            }
        }
        modelMap.addAttribute("id", id);
        Catalog catalog = catalogService.getCatalogById(id);
        StringBuffer albumIds = new StringBuffer();
        if (catalog != null) {
            for (Albums albums : catalog.getAlbums()) {
                if (albumIds.length() == 0) {
                    albumIds.append(albums.getId());
                } else {
                    albumIds.append(",").append(albums.getId());
                }
            }
        }

        if (albumIds.length() != 0) {
            String where = " and i.top !=1 and i.imageAlbum.id in (" + albumIds.toString() + ")";
            modelMap.addAttribute("images", imageService.getImagesByWhere(where, page, 9));
        }
        modelMap.addAttribute("catalogs", catalogs);
        return "forward:/products.jsp";
    }

    @RequestMapping("/subProducts-{id}-{subId}-{page}")
    public String products(ModelMap modelMap, @PathVariable int id, @PathVariable int page, @PathVariable int subId) throws Exception {

        List<Catalog> catalogs = catalogService.getCatalogList();
        modelMap.addAttribute("id", id);
        StringBuffer albumIds = new StringBuffer();
        albumIds.append(subId);
        Albums albums = albumService.getAlbumsById(subId);
        modelMap.addAttribute("album", albums.getName());
        String where = " and i.top!=1 and i.imageAlbum.id in (" + albumIds.toString() + ")";
        modelMap.addAttribute("images", imageService.getImagesByWhere(where, page, 9));
        modelMap.addAttribute("catalogs", catalogs);
        return "forward:/products.jsp";
    }

}
